package com.xywy.tangandroid.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.tangandroid.MyApplication;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.BloodData;
import com.xywy.tangandroid.greendao.BaseDao;
import com.xywy.tangandroid.me.activity.ValidationPswActivity;
import com.xywy.tangandroid.util.SaveLoginPreference;

import de.greenrobot.dao.AbstractDao;

/**
 * 账户
 */
public class AccountAct extends BaseActivity {

	private RelativeLayout left_img;
	private TextView middle_title;
	private RelativeLayout phone_number_relat;
	private RelativeLayout email_address_relat;
	private RelativeLayout password_relat;
	private Button btn_exit;
	private TextView phone_number;
	private AbstractDao<BloodData, Long> blood_data_dao;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_acount);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(getResources()
				.getString(R.string.mine_account_str));

		phone_number_relat = (RelativeLayout) findViewById(R.id.phone_number_relat);
		phone_number = (TextView) findViewById(R.id.phone_number);
		email_address_relat = (RelativeLayout) findViewById(R.id.email_address_relat);
		password_relat = (RelativeLayout) findViewById(R.id.password_relat);
		btn_exit = (Button) findViewById(R.id.btn_exit);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		phone_number_relat.setOnClickListener(this);
		email_address_relat.setOnClickListener(this);
		password_relat.setOnClickListener(this);
		btn_exit.setOnClickListener(this);

	}

	@Override
	protected void initData() {

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences sp = getSharedPreferences("xywy_tang_user",
				context.MODE_PRIVATE);
		phone_number.setText(sp.getString("phonenum", ""));
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;
		case R.id.phone_number_relat:
			Intent intent = new Intent(this, PhoneNumberAct.class);
			intent.putExtra("phone_number", phone_number.getText().toString());
			startActivity(intent);

			break;

		case R.id.password_relat:
			Intent pswintent = new Intent(this, ValidationPswActivity.class);
			startActivity(pswintent);
			break;
		case R.id.btn_exit:// 退出
			exit();
			break;

		default:
			break;
		}
	}

	private void exit() {
		// 清除数据 数据库中的
		BaseDao basedao = BaseDao.getInstance(this);
		blood_data_dao = basedao.getBloodDataDao();
		blood_data_dao.deleteAll();
		SharedPreferences sp = getSharedPreferences("tang_config",
				Context.MODE_PRIVATE);
		sp.edit().clear().commit();
		SharedPreferences sp1 = getSharedPreferences("xywy_tang_user",
				Context.MODE_PRIVATE);
		sp1.edit().clear().commit();
		SharedPreferences sp2 = getSharedPreferences("devicename",
				Context.MODE_PRIVATE);
		sp2.edit().clear().commit();

		SharedPreferences sp3 = getSharedPreferences("config",
				Context.MODE_PRIVATE);
		sp3.edit().clear().commit();

		SharedPreferences sp4 = getSharedPreferences("UserInfo.pre",
				Context.MODE_PRIVATE);
		sp4.edit().clear().commit();

		SaveLoginPreference.saveParam(context, null, null);
		Intent intent = new Intent(context, LoginAct.class);
		startActivity(intent);
		finish();

	}
}
